package com.chanyachting.emall.action.menu;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.chanyachting.emall.domain.Memberopertionmenu;
import com.chanyachting.emall.form.MemberopertionmenuForm;
import com.chanyachting.emall.service.MemberopertionmenuBO;
import com.chanyachting.emall.util.RequestUtil;

public class MemberopertionmenuAction extends DispatchAction {

	
	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		RequestUtil ru=new RequestUtil(request);
		
		ru.setAttribute("memberMenuLit", Memberopertionmenu.list());
		
		return mapping.findForward("list");
	}
	
	
	public ActionForward saveMemberMenu(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		RequestUtil ru=new RequestUtil(request);
		
		//1.获得会员编号
		int memberId=Integer.parseInt(ru.getParameter("memberId", "0"));
		
		//2.获得管理员选中的菜单列表（1-1，2-1，1-2 。。。。）
		
		String menuArray=ru.getParameter("menuArray", "");
		
		if(!menuArray.trim().equals("")){
			
			List<Memberopertionmenu> list=MemberopertionmenuBO.getMemberMenuListByMenuArray(memberId, menuArray);
			
			//先清除原有的菜单
			Memberopertionmenu.deleteByMemberId(memberId);
			
			//保存新的会员菜单
			MemberopertionmenuBO.saveMemberopertionmenuList(list);
			
		}
		
		
		return mapping.findForward("");
	}
	
	
	public ActionForward create(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		RequestUtil ru=new RequestUtil(request);
		
		if(request.getMethod().equals("POST")){
			
			MemberopertionmenuForm mForm=(MemberopertionmenuForm)form;
			
			if(mForm.validate(mapping, request).isEmpty()){
				
				Memberopertionmenu mom=new Memberopertionmenu();
				
				BeanUtils.copyProperties(mom, mForm);
				
				if(mom.save()){
					
					ru.setSuccessMsg();
				}else{
					
					ru.setFailMsg();
				}
			}
		}
		
		
		return mapping.findForward("create");
	}
	
	
	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		
		RequestUtil ru=new RequestUtil(request);
		
		int id=Integer.parseInt(ru.getParameter("id", "0"));
		
		if(request.getMethod().equals("POST")){
			
			MemberopertionmenuForm mForm=(MemberopertionmenuForm)form;
			
			if(mForm.validate(mapping, request).isEmpty()){
				
				Memberopertionmenu mom=new Memberopertionmenu(id);
				
				BeanUtils.copyProperties(mom, mForm);
				
				if(mom.update()){
					
					ru.setSuccessMsg();
				}else{
					
					ru.setFailMsg();
				}
			}
		}
		
		ru.setAttribute("memberMenu", new Memberopertionmenu(id));
		
		return mapping.findForward("update");
	}
	
	
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		RequestUtil ru=new RequestUtil(request);
		
		int id=Integer.parseInt(ru.getParameter("id", "0"));
		
		Memberopertionmenu mom=new Memberopertionmenu(id);
		
		if(mom!=null) mom.delete();
		
		return mapping.findForward("delete");
	}
	
	
	

}
